package cn.sunshine.org.service.impl;

import cn.sunshine.basic.service.impl.BaseServiceImpl;
import cn.sunshine.org.domain.Department;
import cn.sunshine.org.mapper.DepartmentMapper;
import cn.sunshine.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
// 只读事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)


/**
 * @Description: 实现类
 * @Author: Mr.Jiang
 * @Version: 1.0
 * @Date: 2022/6/21 17:13
 */

public class DepartmentServiceImpl extends BaseServiceImpl<Department>  implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /*
     * @Title: save
     * @Description: 保存对象
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/21 17:01
     * @Parameters: [department]
     * @Return void
     */
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        String dirPath = "";
        if (department.getParent() == null) {
            // 如果parent对象为Null,说明他是顶级对象，那么dirPath就是他自己的主键id
            dirPath = "/" + department.getId();
        } else {
            // 否则他就有上级部门，那么我们需要查询到上级部门的对象，获取到上级部门的dirpath，拼接我自己的id，就是我的dirpath
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if (parentDept != null) {
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    /*
     * @Title: update
     * @Description: 修改对象
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/21 17:02
     * @Parameters: [department]
     * @Return void
     */
    @Override
    @Transactional
    public void update(Department department) {
        // 给dirPath拼接值
        String dirPath = "";
        if (department.getParent() == null) {
            // parent对象为Null,说明他是顶级对象dirPath就是他自己的主键id
            dirPath = "/" + department.getId();
        } else {
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if (parentDept != null) {
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }


    /* @Override
     public List<Department> findDepartmentTree() {
         // 查询部门所有的数据集合
         List<Department> departments = departmentMapper.loadAll();
         // 创建一个集合用来装部门数据
         ArrayList<Department> arrayList = new ArrayList<>();
         for (Department first : departments) {
             if (first.getParent_id() == null) {
                 arrayList.add(first);
             } else {
                 for (Department parent : departments) {
                     if (parent.getId() == first.getParent_id()) {
                         parent.getChildren().add(first);
                         break;
                     }
                 }
             }
         }

         return arrayList;
     }*/
    @Override
    public List<Department> findDepartmentTree() {
        // 查询部门所有的数据集合
        List<Department> departments = departmentMapper.loadAll();
        // 创建一个集合用来装部门数据
        List<Department> arrayList = new ArrayList<>();

        // 将集合转换成map集合
        //Map<Long, Department> map = arrayList.stream().collect(Collectors.toMap(Department::getId, item -> item));
        HashMap<Long, Department> map = new HashMap<>();
        for (Department department : departments) {
            map.put(department.getId(), department);
        }
        for (Department first : departments) {
            if (first.getParentId() == null) {
                arrayList.add(first);
            } else {
                Department department = map.get(first.getParentId());
                if (department != null) {
                    department.getChildren().add(first);
                }
            }
        }

        return arrayList;
    }
}
